package com.dotndot.momserver.servlet;

import java.io.*;
import java.util.*;

import javax.jdo.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.dotndot.momserver.*;
import com.dotndot.momserver.data.*;
import com.dotndot.momserver.request.*;
import com.dotndot.momserver.response.*;
import com.dotndot.momserver.utils.*;

@SuppressWarnings( "serial" )
public class CommonServlet extends HttpServlet
{

    @Override
    public void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException
    {
        String path = req.getRequestURI();
        boolean isMultipart = Utils.get().isMultipartContent(req);

        if( isMultipart == false )
        {
            if( path.equals("/common/version.dnd") )
            {
                versionCheck(req, resp);
            }
        }
        else
        {

        }

    }

    /* Version Check */
    public void versionCheck( HttpServletRequest req, HttpServletResponse resp )
    {

        boolean isUpdateFlag = false;
        String secretKey = null;
        Response response = null;
        PrintWriter out = null;

        try
        {
            resp.setContentType("text/plain");
            out = resp.getWriter();
        }
        catch( IOException e )
        {
            e.printStackTrace();
        }

        String appVersion = req.getParameter(Request.VC_APP_VERSION_PARAM);
        String deviceOS = req.getParameter(Request.VC_DEVICE_OS_PARAM);
        String deviceID = req.getParameter(Request.VC_DEVICE_ID_PARAM);

        if( deviceOS.equals(Define.ANDROID_OS) )
        {
            if( Double.parseDouble(Define.ANDROID_APP_VER) > Double.parseDouble(appVersion) )
            {
                isUpdateFlag = true;
            }
        }
        else if( deviceOS.equals(Define.ANDROID_OS) )
        {
            if( Double.parseDouble(Define.IPHONE_APP_VER) > Double.parseDouble(appVersion) )
            {
                isUpdateFlag = true;
            }
        }

        PersistenceManager pm = PMF.get().getPersistenceManager();
        Query query = pm.newQuery(SecretKey.class);
        query.setFilter("deviceID == deviceID_");
        query.declareParameters("String deviceID_");

        @SuppressWarnings( "unchecked" )
        List<SecretKey> keys = (List<SecretKey>) query.execute(deviceID);

        if( keys.size() == 0 )
        {
            secretKey = Utils.get().makeUUID();
            secretKey = secretKey.substring(Define.BEGIN_INDEX_UUID);
            SecretKey item = new SecretKey(deviceID, secretKey);

            pm.makePersistent(item);

        }
        else
        {
            secretKey = keys.get(0).getSecretKey();
        }

        query.closeAll();
        pm.close();

        ResVersion resVersion = new ResVersion(isUpdateFlag, secretKey);
        response = new Response(Response.Status.OK, Response.Code.S000, Response.Message.RET_VERSION_SUCCESS, resVersion);
        out.print(response.toJson());
    }

}
